package com.example.fragment;


import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.adapter.DividerItemDecoration;
import com.example.adapter.PaperAdviceAdapter;
import com.example.beans.PaperAdavice;
import com.example.beans.PaperAdvice1;
import com.example.inter.AdResultCallBack;
import com.example.inter.AdapterCallback;
import com.example.inter.AdviceCallBack;
import com.example.inter.AdviceInfo;
import com.example.inter.LoginCallBack;
import com.example.myapplication.AdviceActivity;
import com.example.myapplication.R;
import com.example.util.AdviceInfoUtil;
import com.example.util.SecondAdviceUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * A simple {@link Fragment} subclass.
 */
public class AdviceFragment extends Fragment implements LoginCallBack, AdapterCallback, AdResultCallBack {

    public RecyclerView recyclerView;
    public List<PaperAdavice> list = new ArrayList<>();
    public PaperAdviceAdapter paperAdviceAdapter;
    public List<PaperAdvice1> list2 = new ArrayList<>();

    public LinearLayoutManager layoutManager;
    public AdviceInfoUtil adviceInfoUtil;
    private NestedScrollView scrollView;
    private int postion = 0;
    private int tag = 0; //是1级评论还是二级评论
    private AdviceActivity adviceActivity;
    private SecondAdviceUtil secondAdviceUtil;

    @Override
    public void onResume() {
        super.onResume();
       /* Bundle bundle = getArguments();
        String paper_id = bundle.getString("paper_id");
        adviceInfoUtil = new AdviceInfoUtil();
        list.clear();
        adviceInfoUtil.getInfo(paper_id, this);*/
        if (list.size() > 0 && tag == 1) {
            secondAdviceUtil = new SecondAdviceUtil();
            secondAdviceUtil.getInfo(list.get(postion).getAdvice_id(), this);
        } else if (list.size() > 0 && tag == 0) {
            Bundle bundle = getArguments();
            String paper_id = bundle.getString("paper_id");
            adviceInfoUtil = new AdviceInfoUtil();
            list.clear();
            adviceInfoUtil.getInfo(paper_id, this);
        }


    }

    @Override
    public void onStart() {
        super.onStart();

    }

    public AdviceFragment() {
        // Required empty public constructor
    }

    public void udpdate(int position) {
        int fristPos = layoutManager.findFirstVisibleItemPosition();
        int lastPos = layoutManager.findLastVisibleItemPosition();

        if (position >= fristPos && position <= lastPos) {
            View view = recyclerView.getChildAt(position);
            if (null != recyclerView.getChildViewHolder(view)) {
                PaperAdviceAdapter.MyViewHolder myViewHolder =
                        (PaperAdviceAdapter.MyViewHolder) recyclerView.getChildViewHolder(view);
                paperAdviceAdapter.update(position, myViewHolder);

            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_advice, container, false);
        init(view);
        paperAdviceAdapter = new PaperAdviceAdapter(getContext(), list, this);

        layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(paperAdviceAdapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(),
                DividerItemDecoration.VERTICAL_LIST));
        Bundle bundle = getArguments();
        String paper_id = bundle.getString("paper_id");
        adviceInfoUtil = new AdviceInfoUtil();
        list.clear();
        adviceInfoUtil.getInfo(paper_id, this);
        return view;
    }

    public void init(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        scrollView = (NestedScrollView) view.findViewById(R.id.scrollView);
    }

    @Override
    public void onSuccess(String info) {
        Log.i("sjsjsj", info);
        List<PaperAdavice> list_teacher = new ArrayList<>();
        List<PaperAdavice> list_student = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(info);
            JSONObject jsonObject1 = jsonObject.optJSONObject("teacher");
            if (jsonObject1 == null) {
                list_teacher = null;
            } else {
                Map valueMap;
                valueMap = getMap4Json(jsonObject1);
                for (Object value : valueMap.values()) {
                    JSONObject jsonObject2 = (JSONObject) value;
                    PaperAdavice paperAdavice = new PaperAdavice();
                    String advice_id = jsonObject2.getString("advice_id");
                    String advice_commit = jsonObject2.optString("advice_commit");
                    String student_number = jsonObject2.optString("student_number");
                    String paper_id = jsonObject2.optString("paper_id");
                    String advice_date = jsonObject2.optString("advice_date");
                    String teacher_number = jsonObject2.optString("teacher_number");
                    String tag = jsonObject2.optString("tag");
                    String ad_id = jsonObject2.optString("ad_id");
                    Boolean bool = false;
                    String teacher_id = null;
                    String teacher_email = null;
                    String teacher_name = null;
                    String teacher_pic = null;
                    String student_pic = null;
                    String student_name = null;
                    String student_email = null;
                    String student_id = null;

                    //false 表明无学生
                    if (student_number.equals("null")) {
                        bool = false;
                        JSONObject jsonObject3 = jsonObject2.optJSONObject("teacher");
                        teacher_id = jsonObject3.optString("teacher_id");
                        teacher_name = jsonObject3.optString("teacher_name");
                        teacher_email = jsonObject3.optString("teacher_email");
                        teacher_pic = jsonObject3.optString("teacher_pic");
                    }
                    if (teacher_number.equals("null")) {
                        bool = true;
                        JSONObject jsonObject3 = jsonObject2.optJSONObject("student");
                        student_id = jsonObject3.optString("student_id");
                        student_email = jsonObject3.optString("student_email");
                        student_name = jsonObject3.optString("student_name");
                        student_number = jsonObject3.optString("student_number");
                        student_pic = jsonObject3.optString("student_pic");
                    }

                    List<PaperAdvice1> list1 = getList(jsonObject2);

                    paperAdavice.setStudent_id(student_id);
                    paperAdavice.setStudent_email(student_email);
                    paperAdavice.setAd_id(ad_id);
                    paperAdavice.setAdvice_commit(advice_commit);
                    paperAdavice.setAdvice_date(advice_date);
                    paperAdavice.setPaper_id(paper_id);
                    paperAdavice.setStudent_name(student_name);
                    paperAdavice.setStudent_pic(student_pic);
                    paperAdavice.setAdvice_id(advice_id);
                    paperAdavice.setTAG(bool);
                    paperAdavice.setTeacher_email(teacher_email);
                    paperAdavice.setTag(tag);
                    paperAdavice.setTeacher_id(teacher_id);
                    paperAdavice.setTeacher_name(teacher_name);
                    paperAdavice.setTeacher_pic(teacher_pic);
                    paperAdavice.setStudent_number(student_number);
                    paperAdavice.setList(list1);
                    list_teacher.add(paperAdavice);
                    Log.i("Tag", paperAdavice.getList().toString());

                }
                list.addAll(list_teacher);

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            JSONObject jsonObject = new JSONObject(info);
            JSONObject jsonObject1 = jsonObject.optJSONObject("student");
            if (jsonObject1 == null) {
                list_student = null;
            } else {
                Map valueMap;
                valueMap = getMap4Json(jsonObject1);
                for (Object value : valueMap.values()) {
                    JSONObject jsonObject2 = (JSONObject) value;
                    PaperAdavice paperAdavice = new PaperAdavice();
                    String advice_id = jsonObject2.getString("advice_id");
                    String advice_commit = jsonObject2.optString("advice_commit");
                    String student_number = jsonObject2.optString("student_number");
                    String paper_id = jsonObject2.optString("paper_id");
                    String advice_date = jsonObject2.optString("advice_date");
                    String teacher_number = jsonObject2.optString("teacher_number");
                    String tag = jsonObject2.optString("tag");
                    String ad_id = jsonObject2.optString("ad_id");
                    Boolean bool = false;
                    String teacher_id = null;
                    String teacher_email = null;
                    String teacher_name = null;
                    String teacher_pic = null;
                    String student_pic = null;
                    String student_name = null;
                    String student_email = null;
                    String student_id = null;

                    //false 表明无学生
                    if (student_number.equals("null")) {
                        bool = false;
                        JSONObject jsonObject3 = jsonObject2.optJSONObject("teacher");
                        teacher_id = jsonObject3.optString("teacher_id");
                        teacher_name = jsonObject3.optString("teacher_name");
                        teacher_email = jsonObject3.optString("teacher_email");
                        teacher_pic = jsonObject3.optString("teacher_pic");
                    }
                    if (teacher_number.equals("null")) {
                        bool = true;
                        JSONObject jsonObject3 = jsonObject2.optJSONObject("student");
                        student_id = jsonObject3.optString("student_id");
                        student_email = jsonObject3.optString("student_email");
                        student_name = jsonObject3.optString("student_name");
                        student_number = jsonObject3.optString("student_number");
                        student_pic = jsonObject3.optString("student_pic");
                    }
                    List<PaperAdvice1> list1 = getList(jsonObject2);
                    paperAdavice.setStudent_id(student_id);
                    paperAdavice.setStudent_email(student_email);
                    paperAdavice.setAd_id(ad_id);
                    paperAdavice.setAdvice_commit(advice_commit);
                    paperAdavice.setAdvice_date(advice_date);
                    paperAdavice.setPaper_id(paper_id);
                    paperAdavice.setStudent_name(student_name);
                    paperAdavice.setStudent_pic(student_pic);
                    paperAdavice.setAdvice_id(advice_id);
                    paperAdavice.setTAG(bool);
                    paperAdavice.setList(list1);
                    paperAdavice.setTeacher_email(teacher_email);
                    paperAdavice.setTag(tag);
                    paperAdavice.setTeacher_id(teacher_id);
                    paperAdavice.setTeacher_name(teacher_name);
                    paperAdavice.setTeacher_pic(teacher_pic);
                    paperAdavice.setStudent_number(student_number);
                    list_student.add(paperAdavice);


                }
                list.addAll(list_student);
                paperAdviceAdapter.notifyDataSetChanged();


            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

    public static Map<String, String> getMap4Json(JSONObject jsonObject) {

        Map valueMap = new HashMap();
        Iterator keyIter = jsonObject.keys();
        String key;
        Object value;

        while (keyIter.hasNext()) {
            key = (String) keyIter.next();
            try {
                value = jsonObject.get(key);
                valueMap.put(key, value);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }


        return valueMap;
    }

    public List<PaperAdvice1> getList(JSONObject jsonObject) {

        List<PaperAdvice1> list1 = new ArrayList<>();

        JSONArray jsonArray = jsonObject.optJSONArray("secondresults");
        if (jsonArray == null) {
            PaperAdvice1 paperAdvice1 = new PaperAdvice1();
            list1.add(paperAdvice1);
        } else for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject1 = jsonArray.optJSONObject(i);
            PaperAdvice1 paperAdavice = new PaperAdvice1();
            String advice_id = jsonObject1.optString("advice_id");
            String advice_commit = jsonObject1.optString("advice_commit");
            String student_number = jsonObject1.optString("student_number");
            String paper_id = jsonObject1.optString("paper_id");
            String advice_date = jsonObject1.optString("advice_date");
            String teacher_number = jsonObject1.optString("teacher_number");
            String tag = jsonObject1.optString("tag");
            String ad_id = jsonObject1.optString("ad_id");
            Boolean bool = false;
            String teacher_id = null;
            String teacher_email = null;
            String teacher_name = null;
            String teacher_pic = null;
            String student_pic = null;
            String student_name = null;
            String student_email = null;
            String student_id = null;

            //false 表明无学生
            if (student_number.equals("null")) {
                bool = false;
                JSONObject jsonObject3 = jsonObject1.optJSONObject("teacher");
                teacher_id = jsonObject3.optString("teacher_id");
                teacher_name = jsonObject3.optString("teacher_name");
                teacher_email = jsonObject3.optString("teacher_email");
                teacher_pic = jsonObject3.optString("teacher_pic");
            }
            if (teacher_number.equals("null")) {
                bool = true;
                JSONObject jsonObject3 = jsonObject1.optJSONObject("student");
                student_id = jsonObject3.optString("student_id");
                student_email = jsonObject3.optString("student_email");
                student_name = jsonObject3.optString("student_name");
                student_number = jsonObject3.optString("student_number");
                student_pic = jsonObject3.optString("student_pic");
            }
            paperAdavice.setStudent_id(student_id);
            paperAdavice.setStudent_email(student_email);
            paperAdavice.setAd_id(ad_id);
            paperAdavice.setAdvice_commit(advice_commit);
            paperAdavice.setAdvice_date(advice_date);
            paperAdavice.setPaper_id(paper_id);
            paperAdavice.setStudent_name(student_name);
            paperAdavice.setStudent_pic(student_pic);
            paperAdavice.setAdvice_id(advice_id);
            paperAdavice.setTAG(bool);
            paperAdavice.setTeacher_email(teacher_email);
            paperAdavice.setTag(tag);
            paperAdavice.setTeacher_id(teacher_id);
            paperAdavice.setTeacher_name(teacher_name);
            paperAdavice.setTeacher_pic(teacher_pic);
            paperAdavice.setStudent_number(student_number);
            list1.add(paperAdavice);

        }
        tag = 0;
        return list1;
    }

    @Override
    public void onFailed() {


    }

    @Override
    public void callBack(int postion, int tag) {

        this.postion = postion;
        this.tag = tag;
    }


    @Override
    public void result(String info) {
        try {
            JSONObject jsonObject = new JSONObject(info);
            list2 = getList(jsonObject);
            list.get(postion).setList(list2);
            udpdate(postion);
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void noResult() {

    }
}
